<template>
  <el-card class="w-100 absolute left-1/2 top-9/20 transform -translate-y-1/2 -translate-x-1/2">
    <p class="text-center pb-6 text-2xl">文件云系统</p>
    <el-form label-position="right" label-width="100px" :model="formLabelAlign" style="max-width: 460px">
      <el-form-item label="账号">
        <el-input v-model="formLabelAlign.account" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="formLabelAlign.password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm()">登录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { httpUserLogin } from '@/api/user'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { setSession } from '@/utils/local'
import { Md5 } from 'ts-md5'
import Crypoto from '@/utils/CryptoJS'
const cry: any = new Crypoto()

const md5: any = new Md5()
const router = useRouter()
const store = useStore()

const formLabelAlign = reactive({
  account: 'test',
  password: '123456',
})

// 登录
const storeLogin = (user: object) => store.commit('login', user)

// 提交登录
const submitForm = () => {
  const encryptPwd = md5.appendAsciiStr(formLabelAlign.password).end()
  const params = {
    account: formLabelAlign.account,
    password: encryptPwd
  }

  // const res:any = await httpUserLogin(params)
  // if(res.code === 200){

  // }
  httpUserLogin(params).then((res: any) => {
    if (res.code === 200) {
      ElMessage.success('登录成功')
      // 存储加密的账号及密码
      const encryptStr = cry.encrypt(JSON.stringify(params))
      setSession('token', encryptStr)
      storeLogin(res.data.user)
      router.push('/')
    } else {
      ElMessage.warning('账号或密码错误')
    }
  })
}


onMounted(() => {
  store
})

</script>
